package com.scsj.supermarket.view.fragment.fivepagemodel;


import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AutoCompleteTextView;
import android.widget.LinearLayout;

import com.scsj.supermarket.R;
import com.scsj.supermarket.adapter.ClassifyAdapter;
import com.scsj.supermarket.base.HomeClassifyBase;
import com.scsj.supermarket.bean.ClassifyBean;
import com.scsj.supermarket.bean.HomeClassifyBean;
import com.scsj.supermarket.customview.MyItemDecoration;
import com.scsj.supermarket.event.FirstEvent;
import com.scsj.supermarket.presenter.HomeClassfiyPresenter;
import com.scsj.supermarket.utils.RxBus;
import com.scsj.supermarket.utils.SkipUtils;
import com.scsj.supermarket.view.fragment.basemodel.BaseFragment;
import com.scsj.supermarket.view.fragment.classifymodel.ClassifyContentFragment;

import java.util.ArrayList;
import java.util.List;

import es.dmoral.toasty.Toasty;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * 分类页面
 */

public class ClassifyFragment extends BaseFragment implements HomeClassifyBase.IView, View.OnClickListener {

    private RecyclerView recyclerView;
    private ClassifyAdapter classifyAdapter;
    private List<ClassifyBean> data;
    private ClassifyContentFragment contentFragment = new ClassifyContentFragment();
    public static String Tag = "分类";
    private HomeClassfiyPresenter classfiyPresenter;
    private View view;
    private LinearLayout mTopSearchLl;
    /**
     * 搜索商品，海量商品任你选
     */
    private AutoCompleteTextView mSearchAt;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_classify, null);
        classfiyPresenter = new HomeClassfiyPresenter(this);
        classfiyPresenter.deliverData();
        initView(view, null);
        initListener();
        initData();
        return view;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        recyclerView = view.findViewById(R.id.recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(getHoldingActivity()));
        recyclerView.addItemDecoration(new MyItemDecoration());
        recyclerView.setHasFixedSize(true);
        recyclerView.setNestedScrollingEnabled(true);

        FragmentTransaction transaction = getHoldingActivity().getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.fragment_container, contentFragment, contentFragment.getClass().getName());
        transaction.commit();
        mTopSearchLl = (LinearLayout) view.findViewById(R.id.top_search_ll);
        mTopSearchLl.setOnClickListener(this);
        mSearchAt = (AutoCompleteTextView) view.findViewById(R.id.search_at);
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {


    }

    @Override
    protected int getLayoutId() {
        return 0;
    }

    @Override
    public void showSuccessMsg(String s, final HomeClassifyBean homeClassifyBean) {

        RxBus.getDefault().toObservable(FirstEvent.class)
                //在io线程进行订阅，可以执行一些耗时操作
                .subscribeOn(Schedulers.io())
                //在主线程进行观察，可做UI更新操作
                .observeOn(AndroidSchedulers.mainThread())
                //观察的对象
                .subscribe(new Action1<FirstEvent>() {
                    @Override
                    public void call(FirstEvent user) {
                        if (user.getCode().equals("ClassifyFragment"))//内容对象创建接收
                            RxBus.getDefault().post(new FirstEvent("ClassifyContentFragment", homeClassifyBean.getData().get(0).getAppGoodsClassPt2VoList(), homeClassifyBean.getData().get(0).getGoodsClassPt1().getTitle()));
                    }
                });

        data = new ArrayList<>();
        for (int i = 0; i < homeClassifyBean.getData().size(); i++) {
            data.add(new ClassifyBean(homeClassifyBean.getData().get(i).getGoodsClassPt1().getTitle()));
        }

        classifyAdapter = new ClassifyAdapter(homeClassifyBean.getData());
        recyclerView.setAdapter(classifyAdapter);
    }

    @Override
    public void showErrorMsg(String s) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.top_search_ll:
                String searchText = mSearchAt.getText().toString().trim();
//                if (TextUtils.isEmpty(searchText)) {
//                    Toasty.info(getHoldingActivity(), "搜索内容不能为空").show();
//                    return;
//                }
//                Toasty.info(SearchActivity.this, "点击事件执行").show();
                SkipUtils.toSearchResultPage(getHoldingActivity(), searchText);
                break;
        }
    }


    //正确的做法
//    private void switchFragment(BaseFragment targetFragment) {
//        FragmentTransaction transaction = getHoldingActivity().getSupportFragmentManager().beginTransaction();
//        if (!targetFragment.isAdded()) {
//            transaction
//                    .hide(classify0Fragment)
//                    .add(R.id.fragment_container, targetFragment)
//                    .commit();
//            System.out.println("还没添加呢");
//        } else {
//            transaction
//                    .hide(classify0Fragment)
//                    .show(targetFragment)
//                    .commit();
//            System.out.println("添加了( ⊙o⊙ )哇");
//        }
//        classify0Fragment = targetFragment;
//    }
}
